// 参数处理类
import {InterfaceUser} from '@/interface/InterfaceUser';
import {InterfaceResponse} from '@/interface/InterfaceResponse';


export default class CommonUnit {

    public static argumentWithUser(user: object = {}, arg: object = {}): object {

        if (JSON.stringify(user) === '{}') {
            return arg;
        } else if ((user as InterfaceUser).Token && (user as InterfaceUser).UserId) {
            return {...(user as InterfaceUser).UserArgument, ...arg};
        } else {
            return arg;
        }
    }

    public static responseLoginVerification(res: InterfaceResponse): boolean {
        if (!res.Success) {
            if (res.Message.indexOf('未登录') >= 0) {
                return false;
            }
        }
        return true;
    }

    public static decodeHtml(s: string): string {
        return s
            .replace(s ? /&(?!#?\w+;)/g : /&/g, '&amp;')
            .replace(/&lt;/g, '<')
            .replace(/&gt;/g, '>')
            .replace(/&quot;/g, '"')
            .replace(/&#39;/g, '\'');
    }

    // 解决微信6.7.4 搭配IOS12+ H5页面里的select,input软键盘弹起的时候页面会上移，
    // 软键盘关闭页面不会下移，导致页面空缺了一部分。
    public static windowSizeRecover(n: number = 0): boolean {
        if (n !== -1) {
            window.scrollTo(0, 0);
        } else {
            window.scrollTo(0, document.documentElement.clientHeight);
        }
        return true;
    }

    // 随机数生成器
    public static randomNumBoth(min: number, max: number): number {
        const range: number = max - min;
        const rand: number = Math.random();
        // 四舍五入
        return (min + Math.round(rand * range));
    }
}
